/**
 * @ 检查一个字符串中是否包含另外一个字符串
 * @ 结果是输入的任何字符或字符串都找不到，看一下那里出错了？
 * @author	 wangshikun <wangshikun2010@gmail.com>
 * @copyright	(c) 2012 wangshikun <wangshikun2010@gmail.com>
 * @license	http://www.opensource.org/licenses/mit-license.php The MIT License
 * @package	default
 * @subpackage	default
 */

#include <stdio.h>
#include <string.h>

// 函数声明
int findstring(const char source[], char pattern[]);

// 主函数
int main(int argc, char *argv[]) {
	const char source[] = {"abcdefghijklmnopqrstuvwxyz"};
	char pattern[26];
	
	printf("\n母字符串：%s\n\n", source);

	printf("请输入要查找的字符串：");
	scanf("%s", pattern);

	if (findstring(source, pattern) != -1) {
		printf("在字符串中找到了子字符串\n");
	} else {
		printf("没有找到\n");
	}

	return 0;
}

// 查找字符串
int findstring(const char source[], char pattern[]) {
	int sl = strlen(source);
	int pl = strlen(pattern);

	for (int i=0; i<sl-pl; i++) {
		int j = 0;

		while (source[i+j] == pattern[j] && j<pl) {
			printf("i=%d, j=%d, source[i+j]=%c, pattern[j]=%c\n", i, j, source[i+j], pattern[j]);
			j++;
		}

		if (j == pl) {
			return i;
		}
	}

	return -1;
}

